package com.hxzy.controller.admin.merchandiser;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.github.pagehelper.PageInfo;
import com.hxzy.common.annotates.RequiredRole;
import com.hxzy.common.constants.MarketConstants;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.resp.R;
import com.hxzy.controller.admin.merchandiser.req.MerchandiserChangePwdReq;
import com.hxzy.controller.admin.merchandiser.req.MerchandiserEditReq;
import com.hxzy.controller.admin.merchandiser.req.MerchandiserSearchReq;
import com.hxzy.controller.admin.merchandiser.req.ProcessReq;
import com.hxzy.controller.admin.merchandiser.resp.MerchandiserResp;
import com.hxzy.service.MerchandiserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api(tags = "后台-商户Api")
@RestController
@RequestMapping(value = "/api/merchandiser")
public class MerchandiserController extends BaseController {

    @Autowired
    private MerchandiserService merchandiserService;


    @RequiredRole(role = MarketConstants.ACCOUNT_ADMIN_TYPE)
    @ApiOperation(value = "商家查询")
    @GetMapping(value = "/data")
    public R<PageInfo<MerchandiserResp>> search(@Valid MerchandiserSearchReq req){
        super.startPage(req);
        List<MerchandiserResp> list=this.merchandiserService.search(req);
        return super.buildPageInfo(list);
    }


    @RequiredRole(role=MarketConstants.ACCOUNT_ADMIN_TYPE)
    @ApiOperation(value = "管理员审核商家信息")
    @PostMapping(value = "/process")
    public R process(@RequestBody @Valid ProcessReq req){
        boolean result=this.merchandiserService.process(req);
        return result?R.ok():R.build(AckCode.DATABASE_ERROR);
    }

    @RequiredRole(role = MarketConstants.ACCOUNT_MERCHANDISER_TYPE)
    @ApiOperation(value ="更新密码")
    @PostMapping(value = "/changePassword")
    public R changePassword(@RequestBody @Valid MerchandiserChangePwdReq req){
         boolean result=this.merchandiserService.changePassword(req);
        return result?R.ok():R.build(AckCode.DATABASE_ERROR);
    }

    @RequiredRole(role = MarketConstants.ACCOUNT_MERCHANDISER_TYPE)
    @ApiOperation(value = "查询当前登录商家的信息")
    @GetMapping(value = "/findById")
    public R<MerchandiserResp> findById(){
        MerchandiserResp resp=this.merchandiserService.findById();
        return R.okHasData(resp);
    }


    @RequiredRole(role = MarketConstants.ACCOUNT_MERCHANDISER_TYPE)
    @ApiOperation(value = "更新商家信息")
    @PutMapping
    public R updateData(@RequestBody @Valid MerchandiserEditReq req){
        boolean result=this.merchandiserService.updateData(req);
        return result?R.ok():R.build(AckCode.DATABASE_ERROR);
    }

}
